package android.test.devicetest;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.test.getintent.GetIntent;
import android.test.until.Max3HWVerion;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ResetGPS extends Activity {

    private Button  reset;
    private Button  next;
    private boolean reboot = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reset_gps);
        reset = (Button) findViewById(R.id.reset);
        next  = (Button) findViewById(R.id.next );
        reset.setOnClickListener(new Onclick());
        next .setOnClickListener(new Onclick());
        next .requestFocus();
    }

    private class Onclick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.next:
                if (reboot)
                {
                    Max3HWVerion.runRootCommand("sync");
                    Max3HWVerion.runRootCommand("reboot");
                }
                else
                {
                    startGPSTestAPK();
                    System.exit(0);
                }
                break;
            case R.id.reset:
                Max3HWVerion.runRootCommand("rm /data/gps/*");
                Toast.makeText(getApplicationContext(), "The GPS ephemeris data has been deleted !", 200).show();
                reboot = true; next.setText("reboot");
                break;
            }
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            finish();
            System.exit(0);
            break;
        }
        return super.onKeyUp(keyCode, event);
    }

    private void startGPSTestAPK()
    {
        Intent i = new Intent();
        ComponentName comp = new ComponentName("com.chartcross.gpstest", "com.chartcross.gpstest.GPSTest");
        i.setComponent(comp);
        try {
            startActivity(i);
        } catch (Exception e) {}
    }
}
